package com.atguigu.springclou.controller;

import com.atguigu.springclou.service.PaymentHistrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHistrixController {
    private static final Logger log = LoggerFactory.getLogger(OrderHistrixController.class);

    @Resource
    private PaymentHistrixService paymentHistrixService;


    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_Ok(@PathVariable(value = "id") Integer id) {
        String result = paymentHistrixService.paymentInfo_Ok(id);
        log.info("******result:" + result);
        return result;
    }


    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    /*@HystrixCommand(fallbackMethod = "paymentInfoTimeOutFallbackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })*/
    @HystrixCommand
    public String paymentInfo_timeout(@PathVariable(value = "id") Integer id) {
//        int a = 10 / 0;
        String result = paymentHistrixService.paymentInfo_timeout(id);
        log.info("******result:" + result);
        return result;
    }

    //兜底方法
    //参数必须与被兜底的方法一致
    public String paymentInfoTimeOutFallbackMethod(@PathVariable(value = "id") Integer id) {
        return "我是消费者80,对方支付系统忙或自己运行错误,稍后再试 o(╥﹏╥)o";
    }

    /**
     * 全局兜底方法
     * 如果特殊的业务没有指定特定的兜底方法
     * 就会执行全局的兜底方法
     *
     * @return
     */
    public String payment_Global_FallbackMethod() {
        return "payment_Global_FallbackMethod 服务降级，稍后再试";
    }
}
